<?php

namespace app\admin_module\controllers;

use app\common\Helpers;
use app\components\exceptions\InvalidRequestException;
use app\models\Admin;
use app\models\ShopCategory;
use app\models\ShopMember;
use Yii;
use yii\db\Exception;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;

class CategoryController extends CommonController
{
    public $layout = 'layout';

    public static $permissionDesc=[
        'index'=>['description'=>'分类列表','role_only'=>[ROLE_AGENT_ONE,ROLE_INTERNET_CAFE]],
        'add'=>'添加管理员'
    ];



    public function actionIndex()
    {
        return $this->redirect(['category/list']);
    }


    public function actionAdd()
    {
        $category = new ShopCategory();

        if (Yii::$app->request->isPost) {
            //post
            $data=Yii::$app->request->post();
            $category->saveCategory($data);

            //跳转列表页面
            return $this->redirect(['category/list']);
        }else{
            return $this->render('add',['model'=>$category]);

        }
    }

    /**
     * 编辑操作
     * @return string|\yii\web\Response
     * @throws InvalidRequestException
     */
    public function actionEdit()
    {

        if (Yii::$app->request->isPost) {
            //提交页
            $category = new ShopCategory();

            //post
            $data=Yii::$app->request->post();
            $category->updateCategory($data);

            //跳转列表页面
            return $this->redirect(['category/list']);
        }else{
            //编辑页

            if(!$id=Yii::$app->request->get('id',0)){
                throw new InvalidRequestException(1000);
            }

            if(!$model=ShopCategory::findOne($id)){
                throw new InvalidRequestException(1012);

            }

            return $this->render('add',['model'=>$model]);
        }
    }

    /**
     * 数据列表
     * @return string
     */
    public function actionList()
    {
        $query = ShopCategory::find()->with('parent');

        return $this->render('list',Helpers::pagination($query,8));
    }

    public function actionDelete()
    {
        if(!$id=Yii::$app->request->get('id')) throw new InvalidRequestException(1000);


    }

}
